package com.hzncc.scaffold.controller.Front.News;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzncc.scaffold.config.MinioConfig;
import com.hzncc.scaffold.core.ResultT;
import com.hzncc.scaffold.entity.Other.News;
import com.hzncc.scaffold.entity.Vo.*;
import com.hzncc.scaffold.service.NewsService;
import com.hzncc.scaffold.service.UserService;
import com.hzncc.scaffold.utils.NumberUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.sql.Struct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: DrunkBear
 * @Date: 2022-06-01 17:16
 * @Description: null
 */
@RestController
@Api(tags = "前台接口-新闻接口")
@RequestMapping("/news")
public class NewsController {

    @Autowired
    NewsService newsService;

    @Autowired
    UserService userService;

    @Autowired
    MinioConfig minioConfig;


    @GetMapping("/detail")
    @ApiOperation("资讯详细信息")
    public ResultT detail(@RequestParam("newsId") Integer newsId) {
        ResultT resultT = newsService.detail(newsId);
        return resultT;
    }

    @GetMapping("/list")
    @ApiOperation("资讯中心列表")
    public ResultT list(Integer page, Integer pageSize, String type,Integer pageNum ) {
        Integer pageData = NumberUtils.pageNum(page, pageNum);
        ArrayList<NewsListVo> newsListVos = new ArrayList<>();

        LambdaQueryWrapper<News> lqw = new LambdaQueryWrapper<>();
        lqw.eq(News::getType, type);
        lqw.eq(News::getIsTrainingClass, 0);
        lqw.eq(News::getStatus, 1);

        lqw.orderByDesc(News::getStayAtTop);
        lqw.orderByDesc(News::getSortTime);
        lqw.orderByDesc(News::getId);

        if (pageData != null && pageSize != null && pageData > 0 && pageSize > 0) {
            PageHelper.startPage(pageData, pageSize);
        }
        List<News> newsList = newsService.list(lqw);
        for (News news : newsList) {
            NewsListVo newsListVo = BeanUtil.copyProperties(news, NewsListVo.class, "attachFile");
            JSONArray jsonArray = new JSONArray(news.getAttachFile());
            List<AttachFileVo> attachFileVoList = JSONUtil.toList(jsonArray, AttachFileVo.class);
            newsListVo.setAttachFile(attachFileVoList);

            UserVo userVo = new UserVo();
            userVo.setId(news.getPublisherId());
            userVo.setName(userService.getById(news.getPublisherId()).getName());

            String thumbnail = "";
            String content = news.getContent();
            List<String> contentTagList = StrUtil.split(content, "\"");
            ArrayList<String> newTagList = new ArrayList<>();

            for (String contentTag : contentTagList) {
                if (StrUtil.contains(contentTag, ".jpg")||StrUtil.contains(contentTag, ".png")) {
                    newTagList.add(contentTag);
                }
            }
            if (CollUtil.isNotEmpty(newTagList)) {
                thumbnail = newTagList.get(0);
            }
            newsListVo.setThumbnail(thumbnail);
            newsListVos.add(newsListVo);
        }


//        PageInfo<NewsListVo> info = new PageInfo<>();
//        info.setList(newsListVos);
//        info.setTotal(new PageInfo<>(newsList).getTotal());
        HashMap<String,Object> map = new HashMap<>();
        map.put("rows",newsListVos);
        map.put("total",new PageInfo<>(newsList).getTotal());


        return ResultT.success(map);
    }
}
